package net.sea.setup.ui.wizard.impl;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.context.AppContext;
import net.sea.commons.util.StringUtils;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.config.custom.CustomConfigItem;
import net.sea.setup.ui.common.SelectFile;
import net.sea.setup.ui.wizard.AbstractWizardPage;

/**
 * 安装路径页面
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class PathPage extends AbstractWizardPage {
	private JTextField destDirTextField = new JTextField();;

	/**
	 * 创建安装路径页面
	 */
	public PathPage() {
		setBorder("\u5B89\u88C5\u8DEF\u5F84");
		setLayout(null);

		JLabel textLb1 = new JLabel(
				"\u5B89\u88C5\u7A0B\u5E8F\u5C06\u5728\u4EE5\u4E0B\u6587\u4EF6\u5939\u4E2D\u5B89\u88C5\u3002");
		textLb1.setBounds(88, 67, 700, 20);
		add(textLb1);

		JLabel textLb2 = new JLabel(
				"<html>\u8981\u5B89\u88C5\u5230\u6B64\u6587\u4EF6\u5939\uFF0C\u8BF7\u5355\u51FB\u201C\u4E0B\u4E00\u6B65\u201D\u3002\u8981\u5B89\u88C5\u5230\u5176\u4ED6\u6587\u4EF6\u5939\uFF0C\u8BF7\u5355\u51FB\u201C\u6D4F\u89C8\u201D\u6216\u76F4\u63A5\u5728\u6587\u672C\u6846\u4E2D\u8F93\u5165\u5B89\u88C5\u8DEF\u5F84\u3002</html>");
		textLb2.setBounds(88, 143, 700, 49);
		View labelView = BasicHTML.createHTMLView(textLb2, textLb2.getText());
		int labelWidth = 200;
		int labelHeight = 100;
		labelView.setSize(labelWidth, labelHeight);
		// 根据限定宽度计算文本内容换行后的真实高度.
		int trueHeight = (int) labelView.getMinimumSpan(View.Y_AXIS);
		textLb2.setPreferredSize(new Dimension(labelWidth, trueHeight));
		add(textLb2);

		JLabel destDirLb = new JLabel("\u76EE\u6807\u6587\u4EF6\u5939\uFF1A");
		destDirLb.setBounds(88, 271, 120, 35);
		add(destDirLb);

		destDirTextField.setBounds(200, 268, 400, 35);
		add(destDirTextField);
		destDirTextField.setColumns(10);

		JButton browseDirBtn = new JButton("\u6D4F\u89C8");
		browseDirBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelectFile selectFile = new SelectFile(
						JFileChooser.OPEN_DIALOG, false,
						JFileChooser.DIRECTORIES_ONLY, "请选择安装路径", "打开");
				selectFile.getFileChooser().setCurrentDirectory(
						new File(AppConstants.SYSTEM_PATH));
				File file = selectFile.getSelectedFile();
				if (file != null) {
					destDirTextField.setText(file.getAbsolutePath());
				}
			}
		});
		browseDirBtn.setBounds(620, 267, 80, 35);
		add(browseDirBtn);
	}

	@Override
	public PageResult validateInput() {
		boolean isNotEmpty = StringUtils.isNotEmpty(destDirTextField.getText()
				.trim());
		return new PageResult(isNotEmpty, isNotEmpty ? "" : "请选择安装目录");
	}

	@SuppressWarnings("unchecked")
	@Override
	protected List<CustomConfig> createCustomConfig() {
		CustomConfig config = new CustomConfig();
		config.setType("平台安装路径");
		CustomConfigItem item = new CustomConfigItem();
		item.setKey("安装路径");
		item.setValue(destDirTextField.getText());
		config.addItem(item);
		
		List<CustomConfig> configs = new ArrayList<CustomConfig>();
		configs.add(config);

		// 保存配置系统到系统上下文
		Map<String, CustomConfig> customCfgMap = (Map<String, CustomConfig>) contextInst
				.getAttribute(AppConstants.CUSTOM_CONFIG_MAP_KEY);
		customCfgMap.put(this.getClass().getName(), config);
		AppContext.getInstance().setAttribute(AppConstants.INSTALL_DIR_KEY,
				destDirTextField.getText());
		return configs;
	}
}
